import javax.jws.soap.SOAPBinding;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author lys
 * 管理用户
 */
public class CloseUser extends JFrame {
    private static CloseUser instance;
    // 标签
    private JLabel jLabel = new JLabel("管理用户");
    private JLabel jLabel2 = new JLabel("请输入用户名：");
    private JLabel jLabel3 = new JLabel(" 姓名 ：");
    private JLabel jLabel4 = new JLabel();
    private JLabel jLabel5 = new JLabel(" 学号 ：");
    private JLabel jLabel6 = new JLabel();
    private JLabel jLabel7 = new JLabel(" 状态 ：");
    private JLabel jLabel8 = new JLabel();
    private JLabel jLabel9 = new JLabel("用户名：");
    private JLabel jLabel10 = new JLabel();

    // 文本框
    private JTextField field = new JTextField(20);
    // 大小
    // private Dimension dimension = new Dimension(220, 20);
    // 按钮
    private JButton button = new JButton("检索");
    private JButton button2 = new JButton("确认封禁");
    private JButton button3 = new JButton("返回");
    private JButton button4 = new JButton("解除封禁");

    // 面板
    private JPanel jPanel = new JPanel();
    // 字体
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font2 = new Font("宋体", Font.BOLD, 30);
    private Font font3 = new Font("宋体", Font.BOLD, 26);

    private CloseUser() {
        // 改变背景图片
        Icon i = new ImageIcon(CloseUser.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        // 标签
        jLabel.setFont(font);
        jLabel.setBounds(280, 35, 800, 100);
        jLabel.setForeground(Color.white);

        jLabel2.setFont(font2);
        jLabel2.setBounds(80, 164, 250, 30);
        jLabel2.setForeground(Color.white);

        // 文本框
        field.setFont(font2);
        field.setBackground(Color.white);
        field.setBounds(300, 163, 250, 40);
        field.setForeground(Color.white);
        field.setOpaque(false);

        // 按钮
        button.setFont(font2);
        button.setBounds(580, 160, 100, 40);
        button.setForeground(Color.white);
        button.setBackground(Color.white);
        button.setOpaque(false);

        // 面板
        jPanel.setBounds(70, 220, 620, 450);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);

        // 面板上标签及按钮
        jLabel3.setFont(font2);
        jLabel4.setFont(font2);
        jLabel5.setFont(font2);
        jLabel6.setFont(font2);
        jLabel7.setFont(font2);
        jLabel8.setFont(font2);
        jLabel9.setFont(font2);
        jLabel10.setFont(font2);

        button2.setFont(font3);
        button3.setFont(font3);
        button4.setFont(font3);

        jLabel3.setForeground(Color.black);
        jLabel4.setForeground(Color.black);
        jLabel5.setForeground(Color.black);
        jLabel6.setForeground(Color.black);
        jLabel7.setForeground(Color.black);
        jLabel8.setForeground(Color.black);
        jLabel9.setForeground(Color.black);
        jLabel10.setForeground(Color.black);

        button2.setForeground(Color.black);
        button3.setForeground(Color.black);
        button4.setForeground(Color.black);

        jLabel9.setBounds(100, 20, 150, 50);
        jLabel10.setBounds(260, 20, 400, 50);
        jLabel3.setBounds(100, 90, 150, 50);
        jLabel4.setBounds(260, 90, 400, 50);
        jLabel5.setBounds(100, 160, 150, 50);
        jLabel6.setBounds(260, 160, 400, 50);
        jLabel7.setBounds(100, 230, 150, 50);
        jLabel8.setBounds(260, 230, 400, 50);

        button2.setBounds(100, 350, 150, 50);
        button4.setBounds(400, 350, 150,50);
        button3.setBounds(550, 650, 90, 50);
        button2.setBackground(Color.white);
        button2.setOpaque(false);
        button3.setBackground(Color.white);
        button3.setOpaque(false);
        button4.setOpaque(false);
        button4.setBackground(Color.white);

        jPanel.add(jLabel3);
        jPanel.add(jLabel4);
        jPanel.add(jLabel5);
        jPanel.add(jLabel6);
        jPanel.add(jLabel7);
        jPanel.add(jLabel8);
        jPanel.add(jLabel9);
        jPanel.add(jLabel10);
        jPanel.add(button2);
        jPanel.add(button4);
        //jPanel.add(button3);
        jPanel.setVisible(false);

        jPanel.setOpaque(false);

        add(jLabel);
        add(jLabel2);
        add(field);
        add(button);
        add(jPanel);
        add(button3);
        add(Label);

        setSize(800,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jPanel.setVisible(true);
                        String username = field.getText();
                        User user = getUser(username);
                        if(user != null){
                            jLabel4.setText(user.getName());
                            jLabel6.setText(user.getSid());
                            jLabel10.setText(username);
                            if(user.getClosed()){
                                jLabel8.setText("封禁");
                            }else{
                                jLabel8.setText("活跃");
                            }
                        }
                    }
                }
        );

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = field.getText();
                closeUser(username);
                setVisible(false);
                ManageUser.getInstance().showUsers();
                AdminInterface.getInstance().admin.setVisible(true);

            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = field.getText();
                openUser(username);
                setVisible(false);
                ManageUser.getInstance().showUsers();
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button.setFocusPainted(false);
        button2.setFocusPainted(false);
        button3.setFocusPainted(false);
    }
    public static CloseUser getInstance() {
        if(instance==null) instance=new CloseUser();
        return instance;
    }

    /**
     *
     * @param username
     * @return 获取特定用户信息
     */
    public User getUser(String username){
        ResultSet rs;
        String sql = "select * from user where username = ?";
        User user = null;
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, username);
            rs = preSql.executeQuery();
            if(!rs.next()){
                JOptionPane.showMessageDialog(null, "数据库中没有该用户！");
                return null;
            }else{
                String name = rs.getString("name");
                String sid = rs.getString("sid");
                String password = rs.getString("password");
                Boolean isClosed = rs.getBoolean("isclosed");
                user = new User(name, sid, username, password, isClosed);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return user;
    }

    /**
     * 封禁用户
     * @param username
     */
    public void closeUser(String username){
        String sql = "update user set isclosed = 1 where username = ?";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, username);
            int ok = preSql.executeUpdate();
            if(ok > 0)
                JOptionPane.showMessageDialog(null, "成功封禁该用户！");
        }catch (Exception e){
            System.out.println(e);
        }
    }
    public static void removeInstance(){
        instance = null;
    }

    /**
     * 解封用户
     * @param username
     */
    public void openUser(String username){
        String sql = "update user set isclosed = 0 where username = ?";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, username);
            int ok = preSql.executeUpdate();
            if(ok > 0)
                JOptionPane.showMessageDialog(null, "成功解封该用户！");
        }catch (Exception e){
            System.out.println(e);
        }
    }
}
